package org.xhnews.download;

import java.io.InputStream;
import java.util.Vector;

import org.xhnews.log.RaspberryLogger;
import org.xhnews.parsers.ContactPullXMLParser;
import org.xhnews.parsers.IndexDataPullXMLParser;
import org.xhnews.parsers.MessagePullXMLParser;
import org.xhnews.parsers.NewsPullXMLParser;
import org.xhnews.parsers.RssPullXMLParser;
import org.xhnews.parsers.WeatherPullXMLParser;
import org.xhnews.preferences.Preferences;
import org.xhnews.to.NewsChannel;
import org.xhnews.to.NewsItem;
import org.xhnews.to.RssChannel;
import org.xhnews.utils.DeviceUtil;
import org.xhnews.utils.StringUtil;

public class DownloadManager {
	/*
	 * 下载新闻频道列表
	 */
	public static Vector downloadNewsChannelList(String url) {
		try {
			InputStream is = Download.get(url);
			Vector channels = NewsPullXMLParser.readXHChannels(is, "utf-8");
			return channels;
		} catch (Exception e) {
			RaspberryLogger.getInstance().info(e.getMessage());
			return null;
		}
	}
	/*
	 * 下载指定新闻频道的所有新闻条目
	 */
	public static Vector downloadNewsItems(String url, NewsChannel newschannel) {
		String channelURL = url+"fn="+newschannel.getLink()+"&imei="+DeviceUtil.getPhoneIMEI();
		try {
			InputStream is = Download.get(channelURL);
			Vector items = NewsPullXMLParser.readXHItems(newschannel,is, "utf-8");
			return items;

		} catch (Exception e) {
			RaspberryLogger.getInstance().info(e.getMessage());
			return null;
		}
	}
	public static Vector downloadVideoItems(String url) {
		try {
			InputStream is = Download.get(url);
			Vector items = NewsPullXMLParser.readVideoItems(is, "utf-8");
			return items;

		} catch (Exception e) {
			RaspberryLogger.getInstance().info(e.getMessage());
			return null;
		}
	}
	
	
	/*
	 * 下载文件并保存为本地文件
	 */
	public static void downloadAndSaveAttachment(String url, String path,
			NewsItem item) {
		try {
			String attach = item.getAttach();
			if (!attach.equalsIgnoreCase("none")) {
				String[] subAttach = StringUtil.split(attach, ';');
				for (int j = 0; j < subAttach.length; j++) {
					int start = subAttach[j].lastIndexOf('/');
					int end = subAttach[j].lastIndexOf(':');
					if (start != -1 && end != -1) {
						String filename = subAttach[j].substring(start+1, end);
						Download.getAndSaveASFile(url, path, filename);
					}
				}
			}
		} catch (Exception e) {
			RaspberryLogger.getInstance().info(e.getMessage());
			e.printStackTrace();
		}
	}
	/*
	 * 下载文件并保存为本地文件
	 */
	public static void downloadAndSavePictures(String url, String path,
			NewsItem item) {
		try {
			String attach = item.getAttach();
			if (!attach.equalsIgnoreCase("none")) {
				String[] subAttach = StringUtil.split(attach, ';');
				for (int j = 0; j < subAttach.length; j++) {
					int start = subAttach[j].lastIndexOf('/');
					int end = subAttach[j].lastIndexOf(':');
					if (subAttach[j].substring(end+1).equals("Image")) {
						if (start != -1 && end != -1) {
							String filename = subAttach[j].substring(start + 1,
									end);
							Download.getAndSaveASFile(url, path, filename);
						}
					}
				}
			}
		} catch (Exception e) {
			RaspberryLogger.getInstance().info(e.getMessage());
			e.printStackTrace();
		}
	}
    /*
     * 下载所有联系人
     */
	public static Vector downloadContacts(String url, String imei) {
		try {
			String contactUrl = url + "IMEI=" + imei;
			InputStream is = Download.get(contactUrl);
			Vector contants = ContactPullXMLParser.readContacts(is, "utf-8");
			return contants;
		} catch (Exception e) {
			RaspberryLogger.getInstance().info(e.getMessage());
			return null;
		}
	}
    /*
     * 下载所有消息
     */
	public static Vector downloadMessages(String url, String imei) {
		try {
			String messageUrl = url + "IMEI=" + imei;
			InputStream is = Download.get(messageUrl);
			Vector messages = MessagePullXMLParser.readMessages(is, "utf-8");
			return messages;
		} catch (Exception e) {
			RaspberryLogger.getInstance().info(e.getMessage());
			return null;
		}
	}
	public static String downloadMessageContent(String url, String imei){
		try{
			String messageUrl = url + "IMEI=" + imei;
			InputStream is = Download.get(messageUrl);
			return StringUtil.is2String(is);
		}catch(Exception e){
			RaspberryLogger.getInstance().info(e.getMessage());
			return null;
		}
	}
	/*
	 * 根据城市，下载天气概要信息
	 */
	public static Vector donwloadWeathers(String url, int citycode) {
		try {
			String cityCode = String.valueOf(citycode);
			String weatherURL = url + cityCode + ".xml";
			InputStream is = Download.get(weatherURL);
			Vector weathers = WeatherPullXMLParser
					.readWeatherItems(is, "utf-8");
			return weathers;
		} catch (Exception e) {
			RaspberryLogger.getInstance().info(e.getMessage());
			return null;
		}
	}
    /*
     * 根据城市，下载天气的详细信息
     */
	public static boolean downloadAndSaveWeatherDetails(String url,
			String path, int citycode) {
		try {
			Download.getAndSaveASFile(url, path,citycode+".html");
			return true;
		} catch (Exception e) {
			RaspberryLogger.getInstance().info(e.getMessage());
			return false;
		}
	}

    /*
     * 根据RSS频道，下载该频道的所有RSS条目
     */
	public static Vector downloadRSSOfChannel(RssChannel rssChannel) {
		try {
			InputStream is = Download.get(rssChannel.getLink());
//			RaspberryLogger.getInstance().info(rssChannel.getLink());
//			RaspberryLogger.getInstance().info(StringUtil.is2String(is));
			Vector items = RssPullXMLParser.readRssItems(is, "utf-8",
					rssChannel.getId());
			return items;
		} catch (Exception e) {
			RaspberryLogger.getInstance().info(e.getMessage());
			e.printStackTrace();
			return null;
		}
	}
	/*
	 * 下载指数数据
	 */
	public static Vector downloadIndexData(String url) {
		try {
			InputStream is = Download.get(url);
			Vector items = IndexDataPullXMLParser.readIndexData(is, "utf-8");
			return items;
		} catch (Exception e) {
			RaspberryLogger.getInstance().info(e.getMessage());
			return null;
		}
	}	
	/*
	 * 发送信息
	 */
	public static InputStream send(String url){
		return Download.get(url);
	}


}
